<?php
namespace app\model;

use think\Model;
use think\facade\Db;

/**
 * 小票打印机模型
 */
class PrinterModel extends Model
{
    /**
     * 获取打印机列表
     */
    public static function get_list()
    {
        $uniacid = current_uni_account();
        $sql = "SELECT * FROM sh_printer WHERE uniacid = {$uniacid} ORDER BY id DESC";
        return Db::query($sql);
    }

    /**
     * 获取打印机详情
     */
    public static function get_info($printer_id)
    {
        $sql = "SELECT * FROM sh_printer WHERE id = {$printer_id}";
        $result = Db::query($sql);
        return $result ? $result[0] : null;
    }

    /**
     * 添加打印机
     */
    public static function add2($data)
    {
        $uniacid = current_uni_account();
        $sql = "INSERT INTO sh_printer SET 
            uniacid = '{$uniacid}',
            name = '{$data['name']}',
            type = '{$data['type']}',
            printer_sn = '{$data['printer_sn']}',
            printer_key = '{$data['printer_key']}',
            api_url = '{$data['api_url']}',
            status = {$data['status']},
            is_default = {$data['is_default']},
            create_time = NOW()";
        
        Db::execute($sql);
        // return Db::getLastInsID();
        return Db::name('sh_printer')->getLastInsID();
    }

    /**
     * 更新打印机
     */
    public static function update2($printer_id, $data)
    {
        $sql = "UPDATE sh_printer SET 
            name = '{$data['name']}',
            type = '{$data['type']}',
            printer_sn = '{$data['printer_sn']}',
            printer_key = '{$data['printer_key']}',
            api_url = '{$data['api_url']}',
            status = {$data['status']},
            is_default = {$data['is_default']},
            update_time = NOW()
            WHERE id = {$printer_id}";
        
        return Db::execute($sql);
    }

    /**
     * 删除打印机
     */
    public static function delete2($printer_id)
    {
        $sql = "DELETE FROM sh_printer WHERE id = " . $printer_id;
        return Db::execute($sql);
    }

    /**
     * 获取默认打印机
     */
    public static function get_default()
    {
        $uniacid = current_uni_account();
        $sql = "SELECT * FROM sh_printer WHERE uniacid = {$uniacid} AND is_default = 1 AND status = 1 LIMIT 1";
        $result = Db::query($sql);
        return $result ? $result[0] : null;
    }

    /**
     * 设置默认打印机
     */
    public static function set_default($printer_id)
    {
        $uniacid = current_uni_account();
        // 先取消所有默认
        $sql = "UPDATE sh_printer SET is_default = 0 WHERE uniacid = {$uniacid}";
        Db::execute($sql);
        
        // 设置新的默认打印机
        $sql = "UPDATE sh_printer SET is_default = 1 WHERE id = {$printer_id}";
        return Db::execute($sql);
    }

    /**
     * 添加打印记录
     */
    public static function add_log($data)
    {
        $uniacid = current_uni_account();
        $sql = "INSERT INTO sh_printer_log SET 
            uniacid = '{$uniacid}',
            printer_id = {$data['printer_id']},
            order_id = {$data['order_id']},
            print_content = '{$data['print_content']}',
            print_result = '{$data['print_result']}',
            print_time = NOW()";
        
        return Db::execute($sql);
    }

    /**
     * 获取打印记录
     */
    public static function get_logs($order_id = 0, $printer_id = 0)
    {
        $uniacid = current_uni_account();
        $where = "WHERE uniacid = {$uniacid}";
        
        if ($order_id > 0) {
            $where .= " AND order_id = {$order_id}";
        }
        
        if ($printer_id > 0) {
            $where .= " AND printer_id = {$printer_id}";
        }
        
        $sql = "SELECT pl.*, p.name as printer_name, o.order_no 
                FROM sh_printer_log pl
                LEFT JOIN sh_printer p ON pl.printer_id = p.id
                LEFT JOIN sh_order o ON pl.order_id = o.id
                {$where}
                ORDER BY pl.id DESC
                LIMIT 100";
        
        return Db::query($sql);
    }
}
